package com.wut.clock.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.wut.clock.aop.log.BusinessType;
import com.wut.clock.aop.log.Log;
import com.wut.clock.common.api.CommonPage;
import com.wut.clock.common.api.CommonResult;
import com.wut.clock.dto.admin.UserParams;
import com.wut.clock.entity.SysRole;
import com.wut.clock.entity.SysUser;
import com.wut.clock.service.IMenuService;
import com.wut.clock.service.IRoleService;
import com.wut.clock.service.IUserService;
import com.wut.clock.util.ShiroUtil;
import com.wut.clock.vo.admin.MenuVo;
import com.wut.clock.vo.admin.UserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Set;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 用户管理
 * @date 2022/5/29 17:01
 */
@Api(value = "用户管理",tags = {"用户管理"})
@RestController
@RequestMapping("/sys/user")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    private final IRoleService roleService;

    private final IMenuService menuService;

    @ApiOperation(value = "新增用户")
    @PostMapping("/addOneUser")
    @RequiresPermissions("sys:user:add")
    @Log(title = "新增用户",businessType = BusinessType.INSERT)
    public CommonResult addOneUser(@RequestBody UserParams userParams){
        int res = userService.insertOneUser(userParams);
        if (res > 0){
            return CommonResult.success("添加用户成功");
        }
        return CommonResult.failed("添加用户失败");
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/deleteOneUser/{userId}")
    @RequiresPermissions("sys:user:delete")
    @Log(title = "删除用户",businessType = BusinessType.DELETE)
    public CommonResult deleteOneUser(@PathVariable(name = "userId") Integer userId){
        if (userService.deleteOneUser(userId)>0)
            return CommonResult.success("删除成功");
        return CommonResult.failed("删除失败");
    }

    @ApiOperation(value = "更新用户")
    @PostMapping("/updateOneUser")
    @RequiresPermissions("sys:user:update")
    @Log(title = "更新用户",businessType = BusinessType.UPDATE)
    public CommonResult updateOneUser(@RequestBody UserParams userParams) {
        int res = userService.updateOneUser(userParams);
        if (res>0) {
            return CommonResult.success("更新用户成功");
        }
        return CommonResult.failed("更新用户失败");
    }

    @ApiOperation("更新头像")
    @PostMapping("/updateAvatar")
    @RequiresPermissions("sys:user:update")
    @Log(title = "更新用户头像",businessType = BusinessType.UPDATE)
    public CommonResult updateAvatar(@RequestParam Integer userId,
                                   @RequestParam String avatar){
//        if(userService.updateAvatar(userId,avatar)>0){
//            return CommonResult.success("更新头像成功");
//        }
        return CommonResult.failed();
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/listUser")
    public CommonResult<CommonPage<SysUser>> getUserList( @RequestParam(required = false) String username,
                                     @RequestParam(required = false) String nickName,
                                     @RequestParam(required = false) String email,
                                     @RequestParam(required = false) String phone,
                                     @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                     @RequestParam(value="pageSize",defaultValue ="10") Integer pageSize){
        List<SysUser> userList = userService.getUserList(username, nickName, email, phone, pageNum, pageSize);
        if (userList != null) {
            return CommonResult.success(CommonPage.restPage(userList),"获取用户列表成功");
        }
        return CommonResult.failed("获取用户列表失败");
    }

    @ApiOperation("根据用户id获取角色信息")
    @GetMapping("/getUserRoleInfo/{userId}")
    public CommonResult getUserRoleInfo(@PathVariable Integer userId){
        SysRole role = roleService.getRoleByUserId(userId);
        if(role == null) return CommonResult.failed("该用户没有角色");
        return CommonResult.success(role);
    }

    @ApiOperation(value = "获取用户的菜单列表")
    @GetMapping("/getUserMenuList")
    @RequiresPermissions("sys:user:query")
    public CommonResult getUserMenuList(){
        List<MenuVo> menuVos = menuService.listMenuVosByUserId(ShiroUtil.getUserId());
        if (CollectionUtils.isNotEmpty(menuVos)){
            return CommonResult.success(menuVos,"获取用户菜单列表成功");
        }
        return CommonResult.failed("此用户暂时没有可访问的菜单");
    }

    @ApiOperation(value = "获得当前登录用户的的权限")
    @GetMapping("/getUserMenuPerms")
    public CommonResult getUserMenuPerms(){
        Set<String> perms = menuService.getMenuPermsByUserId(ShiroUtil.getUserId());
        if (CollectionUtils.isNotEmpty(perms)){
            return CommonResult.success(perms,"获取用户菜单权限列表成功");
        }
        return CommonResult.failed("此用户暂时没有菜单权限");
    }

    @ApiOperation("为用户分配角色")
    @PostMapping("/assignRoleToUser")
    @RequiresPermissions("sys:user:assignRole")
    @Log(title = "分配用户角色",businessType = BusinessType.OTHER)
    public CommonResult resignRoleToUser(@NonNull @RequestParam Integer userId,
                                         @NotBlank @RequestParam Integer roleId){
        int result = userService.assignRole2User(userId, roleId);
        if (result > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("获取当前用户的用户信息和角色信息")
    @GetMapping("getUserInfo")
    public CommonResult getUserInfo(){
        UserInfoVo userInfoVo = userService.getUserInfo();
        return CommonResult.success(userInfoVo);
    }

}
